package com.ByteDance.controller;


import com.ByteDance.model.po.UserInfos;
import com.ByteDance.model.vo.RelationVO;
import com.ByteDance.model.vo.User;
import com.ByteDance.service.IRelationService;
import com.ByteDance.service.IUserService;
import com.ByteDance.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户关系表 前端控制器
 * </p>
 *
 * @author 朱哥
 * @since 2024-03-04
 */
@Api(tags = "RelationController")
@Slf4j
@RestController
@RequestMapping("douyin/relation")
@RequiredArgsConstructor
public class RelationController {
    private final IUserService iUserService;

    private final IRelationService iRelationService;
    /**
     *
     * @param ToUserId 对方用户id
     * @param actionType 1-关注 2-取消关注
     * @return
     */
    @ApiOperation("关系操作接口")
    @PostMapping("/action")
    public RelationVO action(@RequestParam(value = "to_user_id")int ToUserId,
                             @RequestParam(value = "action_type")int actionType,
                             HttpServletRequest req){
        UserInfos userInfos = iUserService.getById(ToUserId);
        if(userInfos == null){//要关注的id不存在
            return RelationVO.fail();
        }

        int userId = JwtUtils.getUserId(req.getHeader("token"));
        Boolean flag = iRelationService.action(userId,ToUserId,actionType);

        if(flag){
            log.info("关注操作成功");
            return RelationVO.success();
        }
        log.info("关注操作失败");
        return RelationVO.fail();
    }

    @ApiOperation("用户关注列表接口")
    @GetMapping("follow/list")
    public RelationVO follow_list(@RequestParam(value = "user_id")int userId){
        UserInfos userInfos = iUserService.getById(userId);
        if(userInfos == null){
            return RelationVO.fail();
        }
        //感觉上面这段代码可以不写
        User[] userList = iRelationService.getFollowList(userId);
        RelationVO relationVO = RelationVO.success();
        relationVO.setUserList(userList);
        log.info("查询用户关注列表成功");
        return relationVO;
    }

    @ApiOperation("用户粉丝列表接口")
    @GetMapping("follower/list")
    public RelationVO follower_list(@RequestParam(value = "user_id")int userId){
        UserInfos userInfos = iUserService.getById(userId);
        if(userInfos == null){
            return RelationVO.fail();
        }
        User[] userList = iRelationService.getFollowerList(userId);
        RelationVO relationVO = RelationVO.success();
        relationVO.setUserList(userList);
        log.info("查询用户粉丝列表成功");
        return relationVO;
    }

    @ApiOperation("用户好友列表接口")
    @GetMapping("friend/list")
    public RelationVO friend_list(@RequestParam(value = "user_id")int userId){
        UserInfos userInfos = iUserService.getById(userId);
        if(userInfos == null){
            return RelationVO.fail();
        }
        User[] userList = iRelationService.getFriendList(userId);
        RelationVO relationVO = RelationVO.success();
        relationVO.setUserList(userList);
        log.info("查询用户好友列表成功");
        return relationVO;
    }
}
